<?php
/**
 * 结果适配
 * 
 * @package SFramework
 * @subpackage App
 * @author Shuky
 * @since v1.0
 * @version $Id: Result.php 95 2011-01-11 18:30:09Z shukyoo@gmail.com $
 */
namespace SF;

class Result
{
    const STATUS = 'status';
    const MESSAGE = 'message';
    
    const VALID = 1;
    const INVALID = 0;
    
    // 结果状态
    protected $_status;
    
    // 结果消息
    protected $_message;
    
    // 自定义结果内容
    protected $_customs = array();
    
    
    
    /**
     * 初始设置
     */
    public function __construct()
    {
    	$this->_status = self::INVALID;
    	$this->_message = '未知错误';
    }
    
    
    /**
     * 设置自定义
     */
    public function __set($key, $value)
    {
    	$this->_customs[$key] = $value;
    }
    
    
    /**
     * 获取自定义
     */
    public function __get($key)
    {
    	return $this->_customs[$key];
    }
    
    
    /**
     * 设置状态
     */
    public function setStatus($status)
    {
    	$this->_status = $status;
    }
    
    
    /**
     * 设置消息
     */
    public function setMessage($message)
    {
    	$this->_message = $message;
    }
    
    
    /**
     * 设置为有效
     */
    public function setValid()
    {
    	$this->_status = self::VALID;
    }
    
    
    /**
     * 判断状态有效性
     */
    public function isValid()
    {
    	return $this->_status === self::VALID;
    }
    
    
    /**
     * 获取状态
     */
    public function getStatus()
    {
    	return $this->_status;
    }
    
    
    /**
     * 获取消息
     */
    public function getMessage()
    {
    	return $this->_message;
    }
    
    
    /**
     * 获取数组形式的结果集
     */
    public function getByArray()
    {
    	return array_merge(array(self::STATUS => $this->getStatus(), self::MESSAGE => $this->getMessage()), $this->_customs);
    }
    
    
    /**
     * 获取json形式的返回结果数组
     */
    public function getByJson()
    {
    	return json_encode($this->getByArray());
    }
}